<?php

namespace Ifmia\AppBundle\Controller;

//use Symfony\Component\BrowserKit\Response;
use Symfony\Component\HttpFoundation\Response;

use Symfony\Component\Validator\Constraints\DateTime;

use Symfony\Component\Validator\Constraints\Date;

use Symfony\Component\BrowserKit\Request;

use Ifmia\AppBundle\Entity\Session;
use Ifmia\AppBundle\Form\SessionType;
use Ifmia\AppBundle\Entity\Formation;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;


class SessionController extends Controller
{
   //Ajouter Session//
   
    public function ajouterAction()
    {	
    	$session = new Session; 
    	
    	$code = $session->generateCode();
    	
    	$form = $this->createForm(new SessionType(), $session);
    	$form['codeSession']->setData($code);
    	
    	$request = $this->container->get('request');
    	if ($request->getMethod() == 'POST')
    	{
    		$form->bindRequest($request);
    		if ($form->isValid())
    		{
    			$em = $this->container->get('doctrine')->getEntityManager();
    			
    			$session-> setEtatSession("planifiee");
    			$session-> setDatePlanificationSession(new \DateTime());
    			$em->persist($session);
    			$em->flush();
    		}
    	}
    	
    	
        return $this->render('IfmiaAppBundle:Session:AjouterSession.html.twig',array('form' => $form->createView()));
    }
    
    
    //Modifier Session//
    public function listeAction($page)
    {
    	$repository = $this->getDoctrine()->getEntityManager()->getRepository('IfmiaAppBundle:Session');
    	
    	$nb_session = $repository->getNombreSession();
    	
    	$nb = 2 ;
    	
    	if($nb_session == 0) $nb_pages = 1;
		else $nb_pages = ceil($nb_session/$nb);
		$offset = ($page-1) * $nb;
    	
    	if( $page < 1 OR $page > $nb_pages )
    	{
    		throw $this->createNotFoundException('Page inexistante (page = '.$page.')');
    	}
    	
    	$session = $repository->findBy(array(),
    			array(),
    			$nb,
    			$offset 
    	);
    	
    	
    	return $this->render('IfmiaAppBundle:Session:listeSession.html.twig',array('sessions' => $session,'page'=> $page, 'nb_pages'=> $nb_pages));
    }
    
    public function modifierAction()
    {
    	$id = $this->getRequest()->query->get('id');
    	
    	$se = $this->getDoctrine()->getEntityManager();
    	
    	$session = $se->find('IfmiaAppBundle:Session', $id);
    	
	    	if( ! $session)
	    	{
	    		throw $this->createNotFoundException('Session[id='.$id.'] inexistant');
	    	}
	    	
	    $form = $this->createForm(new SessionType(), $session);
	    $request = $this->getRequest();
	    
		    if($request->getMethod() == 'POST'){
		    	$form->bindRequest($request);
			    	if($form->isValid()){
			    		$se->persist($session);
			    		$se->flush();
			    	}
		    }
		    return $this->render('IfmiaAppBundle:Session:AjouterSession.html.twig', array('form'=>$form->createView(),'session'=>$session));
    }
    
    public function supprimerAction()
    {
    	$id = $this->getRequest()->request->get('id');
    	$em = $this->getDoctrine()->getEntityManager();
    	
    	$session = $em->getRepository('IfmiaAppBundle:Session')->find($id);
    	$em->remove($session);
    	$em->flush();
    	return new Response('TRUE');
    }
    
   public function changeSessionAnAction()
   {
   		
   	
	   	$id = $this->getRequest()->request->get('id');
	   	$em = $this->getDoctrine()->getEntityManager();
	   	$session = $em->getRepository('IfmiaAppBundle:Session')->find($id);
	   	
	   	$session -> setEtatSession('annulee');
	   	$session -> setDateAnnulationSession(new \DateTime());
	   	$em->persist($session);
	   	$em->flush();	   	
	   	
	   	$response = new Response(json_encode(array('code' => $session->getCodeSession())));
	   	$response->headers->set('Content-Type', 'application/json');
	   	
	   	return $response;
	   	
	   	
   }
    
   public  function chercherAction()
   {
   		$etat = $this->getRequest()->request->get('etat');
   		$sessions = null;
   		if(!strcmp($etat, 'tous') == 0)
	   		$sessions = $this->getDoctrine()->getEntityManager()->getRepository('IfmiaAppBundle:Session')->cherhcerSession($etat);
	   else 
	   		$sessions = $this->getDoctrine()->getEntityManager()->getRepository('IfmiaAppBundle:Session')->findAll();
   		
   		
	   $table = array();
	   
	   foreach ($sessions as $s){
	   	array_push($table, array(
	   		$s->getId(), 
	   		$s->getCodeSession(), 
	   		$s->getNombreHeureSession(), 
	   		$s->getDateDebutSession()->format('d-m-Y'), 
	   		$s->getDateFinSession()->format('d-m-Y'),
	   		$s->getFormation()->getIntituleFormation(),
	   		$s->getTypologieSession()->getIntituleTypologie(),
	   	));
	   }
	   
	   $response = new Response(json_encode($table));
	   $response->headers->set('Content-Type', 'application/json');
	    
	   return $response;
	   
   }
   
   
   
   
    
}